<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" media="screen" href="../../layui/css/layui.css">
</head>
<body>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 col-md-offset-2 col-lg-offset-2">
	<form class="form-horizontal" onsubmit="return false" id="form">
		<fieldset>
			<input type="hidden" id="id" name="id">
            <div class='form-group'>
                <label class='col-md-4 control-label'>所属部门</label>
                <div class='col-md-8'>
                    <input class='form-control' type='hidden' name='dept.id' id='deptId'>
                    <input style="width: 90%; float: left" class='form-control' type='text'
                           name='dept.name' id='deptName' placeholder='所属部门'
                           data-bv-notempty='true' data-bv-notempty-message='所属部门 不能为空' readonly >
                    <button style="width: 10%;float: right; height: 32px; line-height: 32px;" class="layui-btn layui-btn-sm"
                            onclick="showTree('deptId', 'deptName', 'dept', '部门', '总公司', 'select')">
                        <i class="layui-icon">&#xe615;</i>
                    </button>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-4 control-label">用户名</label>
                <div class="col-md-8">
                    <input class="form-control" placeholder="用户名" type="text" name="username" id="username"
                           data-bv-notempty="true" data-bv-notempty-message="用户名 不能为空"
                           data-bv-stringlength="true" data-bv-stringlength-min="2" data-bv-stringlength-max="32" data-bv-stringlength-message="长度2-32位"
                           data-bv-regexp="true" pattern="^[a-z0-9_]+$" data-bv-regexp-message="用户名只能为字母数字下划线">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-4 control-label">密码</label>
                <div class="col-md-8">
                    <input class="form-control" placeholder="密码如果为空则不重置密码" type="text" value="" name="password" id="password" >
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-4 control-label">昵称</label>
                <div class="col-md-8">
                    <input class="form-control" placeholder="昵称" type="text" name="nickname" id="nickname"
                           required data-bv-notempty-message="昵称必填">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-4 control-label">手机号</label>
                <div class="col-md-8">
                    <input class="form-control" placeholder="手机号" type="text" maxlength="11" name="phone" id="phone"
                           pattern="[0-9]+" data-bv-regexp-message="手机号必须是数字">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-4 control-label">电话</label>
                <div class="col-md-8">
                    <input class="form-control" placeholder="电话" type="text" maxlength="20" name="telephone" id="telephone">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-4 control-label">邮箱</label>
                <div class="col-md-8">
                    <input class="form-control" placeholder="邮箱" type="text" maxlength="50" name="email" id="email">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-4 control-label">生日</label>
                <div class="col-xs-2">
                  <input type="text" class="form-control" placeholder="生日" id="birthday" name='birthday'>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-4 control-label">性别</label>
                <div class="col-md-8" style="width: 100px">
                    <select class="form-control input-sm" name="sex" id="sex">
                    </select>
                </div>
            </div>
            <div class="form-group" id="statusDiv">
                <label class="col-md-4 control-label">状态</label>
                <div class="col-md-8" style="width: 100px">
                    <select class="form-control input-sm" name="status" id="status">
                    </select>
                </div>
            </div>
            <div class="form-group layui-form-item" id = "roleDiv">
                <!--<div class="form-group layui-form-item" id="rolesDiv">
				<label class="col-md-4 control-label layui-form-label">角色</label>
				<div class="checkbox layui-input-block" id="roles"></div>
			</div>-->
            </div>

			<div class="form-actions">
				<div class="row" align="center">
					<div class="col-md-12">
						<!--<button class="btn btn-primary" onclick="location.href='userList.html'">返回</button>-->
						<button class="btn btn-primary" type="submit" onclick="save()">
							<i class="fa fa-save"></i> 保存
						</button>
					</div>
				</div>
			</div>

		</fieldset>
	</form>
</div>
	<script type="text/javascript" src="../../js/libs/jquery-2.1.1.min.js"></script>
	<script type="text/javascript" src="../../js/jq.js"></script>
	<script type="text/javascript" src="../../js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
	<script type="text/javascript" src="../../js/common.js"></script>
	<script type="text/javascript" src="../../js/my/roles/role.js"></script>
	<script type="text/javascript" src="../../layui/layui.js"></script>
	<script type="text/javascript" src="../../js/dict.js"></script>
	<script type="text/javascript">
        layui.use(['layer','laydate'], function(){
            var layer = layui.layer;
            var laydate = layui.laydate;
            laydate.render({
               elem: '#birthday'
            });
        });

        showDictSelect("sex", "sex");
        showDictSelect("status", "userStatus");
        initRoles(getDict('modular'));

        initData();

        function initData(){
            var id = getUrlParam("id");
            if(id != ""){
                $.ajax({
                    type : 'get',
                    url : '/users/'+id,
                    async : false,
                    success : function(data) {
                        $("#id").val(data.id);
                        $("#deptId").val(data.dept.id);
                        $("#deptName").val(data.dept.name);
                        $("#username").val(data.username);
                        $("#nickname").val(data.nickname);
                        $("#phone").val(data.phone);
                        $("#telephone").val(data.telephone);
                        $("#email").val(data.email);
                        $("#birthday").val(data.birthday);
                        $("#sex").val(data.sex);
                        $("#status").val(data.status);
                        var roleList = data.roleList;
                        if(roleList){
                            $.each(roleList, function (i, role) {
                                var modular = role.modular;
                                var roleId = role.id;
                                let str = "input:radio[name='" + modular + "roleId'][value=" + roleId + "]";
                                console.log(str);
                                $(str).attr('checked', true);
                            })
                        }
                    }
                });

                initRoleDatas(id);
            }
        }

		function save() {
            $('#form').bootstrapValidator();
			var bootstrapValidator = $("#form").data('bootstrapValidator');
			bootstrapValidator.validate();
		    if(!bootstrapValidator.isValid()){
			   return;
		    }

		    var formdata = $("#form").serializeObject();
		    formdata.roleIds = getCheckedRoleIds();
            //console.log(JSON.stringify(formdata));

			$.ajax({
				type : 'put',
				url : '/users',
				contentType: "application/json; charset=utf-8",  
				data : JSON.stringify(formdata),
				success : function(data) {
                    layer.msg("修改成功", {shift: -1, time: 1000}, function(){
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                    });
                }
			});
		}
		
	</script>
</body>
</html>